package com.diyunapp.xilinapp.details;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.diyunapp.xilinapp.bean.StoreListBean;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.SearchDBHelper;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by NIDAYE on 2016/8/5.
 */
public class GetAllTypeListService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("zhangxiaoxidebug","GetAllTypeListService onStartCommand");
        getTypeList();
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    private void getTypeList() {
        String url = "http://www.xilingou.com/mobile/index.php?act=goods&op=alltype";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(2000);
        http.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        int result = parseProject(responseInfo.result);
                        switch (result) {
                            case 1:
                                break;
                            case 0:
                                break;
                            default:
                                break;
                        }
                    }

                    @Override
                    public void onStart() {
                    }

                    private int parseProject(String result) {
                        Log.i("zhangxiaoxidebug", "store_result=" + result);
                        try {
                            JSONObject jsObjectCode = new JSONObject(result);
                            int code = jsObjectCode.getInt("code");

                            if (code != 200) {
                                return 0;
                            }

                            JSONArray jsArrayData = jsObjectCode.getJSONArray("data");

                            if(jsArrayData.length()-30 > MainActivity.myDBHelper.contentNum())
                            {
                                MainActivity.isinFreshing = true;
                                MainActivity.myDBHelper.clearContent();
                            }
                            for(int i=0;i<jsArrayData.length();i++)
                            {
                                JSONObject jsObjectStoreInfo = jsArrayData.getJSONObject(i);
                                String type_id = jsObjectStoreInfo.getString("type_id");
                                String type_name = jsObjectStoreInfo.getString("type_name");
                                MainActivity.myDBHelper.insertContent(type_id,type_name);
                            }
                            MainActivity.isinFreshing = false;
                            stopSelf();
                        } catch (JSONException e) {
                            e.printStackTrace();
                            return 0;
                        }
                        return 1;
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        Log.i("zhangxiaoxidebug", "typelist....onFailure msg=" + msg);
                    }
                }
        );
    }
}
